package cn.livingCloud.controller.user.validator;

import cn.livingCloud.dto.SettingFromDto;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by DingJie  on 2016/1/19.
 */
@Component
public class SettingFromDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> clazz) {
        return SettingFromDto.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        SettingFromDto fromDto = (SettingFromDto) target;
        validateFrom(fromDto, errors);
    }

    private void validateFrom(SettingFromDto fromDto, Errors errors) {
        if (StringUtils.isEmpty(fromDto.getName())) {
            errors.rejectValue("name", null, "应用名称不能为空");
        }
        if (StringUtils.isEmpty(fromDto.getAppid())) {
            errors.rejectValue("appid", null, "微信appid不能为空");
        }
        if (StringUtils.isEmpty(fromDto.getSecret())) {
            errors.rejectValue("secret", null, "微信secret不能为空");
        }
        if (StringUtils.isEmpty(fromDto.getToken())) {
            errors.rejectValue("token", null, "微信token不能为空");
        }
    }
}
